<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @copyright essentialmind.com 2005
    ***/
    class shoppingCart{

        public $items = array();
        protected $db;

        function __construct(ADOConnection $db){
            $this->db = $db;
        }
        
        public function clear(){
            $this->items = array();
        }        
        
        public function addItem($aid,$q1,$q2){
            $type = $this->db->getOne("SELECT id FROM datatype WHERE lower(codeset) = 'shoparticle'");
            $art  = $this->db->getRow("SELECT tax, price, price_kids, image FROM node_shop_article WHERE id = ".$aid);
            $item = new shoppingCartItem($aid,$q1,$q2,$art['price'],$art['price_kids']);
            $item->set('name',$this->db->getOne("SELECT title FROM pagemap WHERE pagetype = $type AND pageid = ".(int)$aid));
            $item->set('url',$this->db->getOne("SELECT get_path(nodeid)||pageurl||'.html' AS url FROM pagemap WHERE pagetype = $type AND pageid = ".(int)$aid));
            $item->set('tax',$art['tax']);
            
            //an image?
            if($art['image'] == 't'){
                $item->set('image',true);
                $item->set('image_path','files'.DIRECTORY_SEPARATOR.'pages'.DIRECTORY_SEPARATOR.$type.'__'.$aid.DIRECTORY_SEPARATOR);
            }
            
            $this->items[$aid] = $item;
        }
        
        
        
        public function dropItem($aid){
            unset($this->items[$aid]);
        }
        
        
        
        public function count(){
            return count($this->items);
        }
        
        
        public function discount(){
            if(count($this->items) > 3){
                return -15;
            }elseif(count($this->items) > 2){
                return -10;
            }elseif(count($this->items) > 1){
                return -5;
            }
            return 0;            
        }
        
        public function sum(){
            $p = 0;
            foreach($this->items as $item){
                $p = $p + $item->price();
            }
            return (float)$p;
        }
        
        public function price(){
            $p = 0;
            foreach($this->items as $item){
                $p = $p + $item->price();
            }
            
            $discount = $this->discount();
            
            return (float)$p * (1 + $discount / 100);
        }
        
        
        
        public function smarty(){
            $array = array();            
            foreach($this->items as $t){
                $item  = array();
                $item['name']  = $t->get('name');
                $item['url']   = $t->get('url');
                $item['image'] = $t->get('image');
                $item['image_path'] = $t->get('image_path');
                $item['q1']    = $t->quant();
                $item['q2']    = $t->quant2();
                $item['price'] = $t->price();
                $id            = $t->aid();
                $item['id']    = $id;
                $array[$id]    = $item;
            }
            return $array;
        }
        
        
        public function getTransportCosts(){
            return 0;
        }
        
        
        
        public function item($id){
            $t = $this->items[$id];
            $item = array();
            if(is_object($t)){
                $item['name']  = $t->get('name');
                $item['url']   = $t->get('url');
                $item['q1']    = $t->quant();
                $item['q2']    = $t->quant2();
                $item['price'] = number_format($t->price(),2,'.','');
                $item['price1']= number_format($t->price1(),2,'.','');
                $item['price2']= number_format($t->price2(),2,'.','');
                $item['id']    = $t->aid();      
            }
            
            //ulaut tweak
            $string = iconv('utf-8','iso-8859-1',$item['name']);
            $string = str_split($string);
            $toReturn = array();
            foreach($string as $key => $val){
                $t = ord($val);
                switch($t){
                    case 252: $toReturn[] = '&uuml;'; break;
                    case 246: $toReturn[] = '&ouml;'; break;
                    case 228: $toReturn[] = '&auml;'; break;
                    default:  $toReturn[] = $val;
                }
                
                //echo ord($val).'-'.$val.' ';
            }
            $item['name'] = implode('',$toReturn);
    
            return $item;
        }
        
        
        
        function __wakeup(){
            global $db;
            $this->db = $db;
        }
        
        function getItemsList(){
            return array_keys($this->items);
        }
    }
    
    
?>